import java.io.UnsupportedEncodingException;
import java.security.InvalidKeyException;
import java.security.KeyFactory;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.NoSuchAlgorithmException;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.spec.InvalidKeySpecException;
import java.security.spec.X509EncodedKeySpec;

import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;


public class RSAObject {
	KeyPairGenerator keyPairGenerator;
	KeyPair keyPair;
	PublicKey publicKey;
	PrivateKey privateKey;
	Cipher cipher;
	
	public RSAObject() {
		try {
			keyPairGenerator = KeyPairGenerator.getInstance("RSA");
			keyPair = keyPairGenerator.generateKeyPair();
			publicKey = keyPair.getPublic();
			privateKey = keyPair.getPrivate();
			
			cipher = Cipher.getInstance("RSA/ECB/PKCS1PADDING");
		} catch (NoSuchAlgorithmException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (NoSuchPaddingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public void setKeyForEncyption(byte[] encodedKey) {
		X509EncodedKeySpec pubKeySpec;
		KeyFactory keyFactory;
		try {
			pubKeySpec = new X509EncodedKeySpec(encodedKey);
			keyFactory = KeyFactory.getInstance("RSA");
			publicKey = keyFactory.generatePublic(pubKeySpec);
		} catch (NoSuchAlgorithmException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InvalidKeySpecException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public byte[] encrypt(String message) {
		byte[] encryptedMessage = null;
		try {
			cipher.init(Cipher.ENCRYPT_MODE, publicKey);
			encryptedMessage =  cipher.doFinal(message.getBytes("UTF8"));
		} catch (IllegalBlockSizeException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (BadPaddingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InvalidKeyException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return encryptedMessage;
	}
	
	public String decrypt(byte[] encodedMessage) {
		String decodedMessage = null;
		
		if (privateKey == null) return null;
		
		try {
			cipher.init(Cipher.DECRYPT_MODE, privateKey);
			decodedMessage = new String(cipher.doFinal(encodedMessage), "UTF8");
		} catch (InvalidKeyException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalBlockSizeException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (BadPaddingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return decodedMessage;
	}
	
	public byte[] getMyPublicKey() {
		
		return publicKey.getEncoded();
	}
}
